% Read and plot accelerometer data from an Arduino via serial
% communications

clear all;

X_AXIS = 0;
Y_AXIS = 1;
Z_AXIS = 2;

delete(instrfind);

comPortName = 'COM6';

port = serial(comPortName);
%set(port, 'Timeout');

fopen(port);

plot(1);
hold on;
counter = 1;

prevX = 0; prevY = 0; prevZ = 0;

pause(3); % Gotta have a pause betwen the port opening and port reading

while (true)
    fwrite(port, 1);
    pause(0.06);
    receivedBytes = fread(port, 3);
    
    xValues(counter) = receivedBytes(1);
    yValues(counter) = receivedBytes(2);
    zValues(counter) = receivedBytes(3);
    %sum(counter)= xValues(counter) + yValues(counter) + zValues(counter);
    
    plot(xValues, 'Color', 'red');
    plot(yValues, 'Color', 'blue');
    plot(zValues, 'Color', 'green');
    %plot(sum, 'Color', 'yellow', 'LineWidth', 2)
    
    counter = counter + 1;
    axis([0 counter 0 500]);
end

fclose(port);